package com.weapp.common;

import io.swagger.annotations.ApiModelProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;

/**
 * 接口返回对象实体
 *
 */
@SuppressWarnings(value = { "unchecked", "rawtypes" })
public class RestResponse<T> extends Response {

  private static final Logger logger = LoggerFactory.getLogger(RestResponse.class);

    private static final long serialVersionUID = 1L;
    @ApiModelProperty(
        value = "code值",
        required = true
    )
    private String code;
    @ApiModelProperty(
        value = "消息",
        required = true
    )
    private String message;
    @ApiModelProperty("返回对象")
    private T data;

    public RestResponse() {
        this.code = String.valueOf(HttpStatus.OK.value());
        this.message = HttpStatus.OK.getReasonPhrase();
    }

    public static <T> RestResponse.RestResponseBuilder<T> builder() {
        return new RestResponse.RestResponseBuilder();
    }

    public void setCode(final String code) {
        this.code = code;
    }

    public void setMessage(final String message) {
        this.message = message;
    }

    public void setData(final T data) {
        this.data = data;
    }

    public String getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

    public T getData() {
        return this.data;
    }

    public static class RestResponseBuilder<T> {
        private RestResponse<T> target = new RestResponse();

        public RestResponseBuilder() {
        }

        public RestResponse.RestResponseBuilder<T> success(boolean success) {
            this.target.setSuccess(success);
            return this;
        }

        public RestResponse.RestResponseBuilder<T> code(String code) {
            this.target.setCode(code);
            return this;
        }

        public RestResponse.RestResponseBuilder<T> message(String message) {
            this.target.setMessage(message);
            return this;
        }

        public RestResponse.RestResponseBuilder<T> data(T data) {
            this.target.setData(data);
            return this;
        }

        public RestResponse<T> build() {
            return this.target;
        }
    }
}
